package com.meila.cms.controller;

import java.text.SimpleDateFormat;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.meila.cms.auth.AuthClass;
import com.meila.cms.entity.User;
import com.meila.cms.model.CommonModel;
import com.meila.cms.model.ResetPwdModel;
import com.meila.cms.model.UserModel;
import com.meila.cms.services.ISettingService;

@RequestMapping("/front/setting")
@Controller
@AuthClass
public class SettingController {
	
	private ISettingService settingService;
	
	public ISettingService getSettingService() {
		return settingService;
	}

	@Inject
	public void setSettingService(ISettingService settingService) {
		this.settingService = settingService;
	}
	
	@RequestMapping(value = "/accountSet", method = RequestMethod.GET)
	public String accountSet(Model model,HttpSession session) {
		try{
			User user = (User)session.getAttribute("loginUser");
			
			if(user!=null){
				model.addAttribute("user", user);
				return "/setting/accountSet";
			}else{
				return "/index/login";
			}			
		}catch(Exception e){
			e.printStackTrace();
			return "/index/login";
		}
				
	}
	
	@RequestMapping(value = "/approveContent", method = RequestMethod.GET)
	public String approveContent(Model model,HttpSession session){
		try{
			User user = (User)session.getAttribute("loginUser");
			
			if(user!=null){
				if(user.getEstablishmentDate()!=null){
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					String esTime=format.format(user.getEstablishmentDate());
					model.addAttribute("esTime", esTime);
				}
				model.addAttribute("user", user);
				return "/setting/approveContent";
			}else{
				return "/index/login";
			}
		}catch(Exception e){
			e.printStackTrace();
			return "/index/login";
		}
	}
	
	@RequestMapping(value = "/updatePassContent", method = RequestMethod.GET)
	public String updatePassContent(Model model,HttpSession session){
		try{
			User user = (User)session.getAttribute("loginUser");
			
			if(user!=null){
				model.addAttribute("user", user);
				return "/setting/updatePassContent";
			}else{
				return "/index/login";
			}
		}catch(Exception e){
			e.printStackTrace();
			return "/index/login";
		}
	}
	
	@RequestMapping(value = "/saveBaseInfo", method = RequestMethod.POST)
	@ResponseBody
	public CommonModel saveBaseInfo(User user){
		
		return settingService.saveBaseInfo(user);
		
	}
	
	@RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
	@ResponseBody
	public CommonModel updatePwd(ResetPwdModel resetPwdModel){
		
		return settingService.updatePwd(resetPwdModel);
		
	}
	
	@RequestMapping(value = "/personalConfirm", method = RequestMethod.POST)
	@ResponseBody
	public CommonModel updatePersonal(UserModel userModel){
		
		return settingService.updatePersonal(userModel);
		
	}
	
	@RequestMapping(value = "/firmConfirm", method = RequestMethod.POST)
	@ResponseBody
	public CommonModel updateFirm(UserModel userModel){
		
		return settingService.updateFirm(userModel);
		
	}

}
